package switchviz.core.mib;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JXMLComboBox;
import switchviz.gui.component.PaddingPanel;
import switchviz.network.NetworkException;
import switchviz.network.NetworkFacade;
import switchviz.network.netswitch.SwitchDataModel;

public class MIBTestPanel extends PaddingPanel implements ActionListener, ItemListener 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6582759285843861356L;
	private static Logger logger = Logger.getLogger(MIBTestPanel.class);
	private JLabeldComponent _lblname = null;
	private JLabeldComponent _lbloid = null;
	private JLabeldComponent _cmbtestport = null;
	private JXMLComboBox _cmbswitch = null;
	private JLabeldComponent _txttest = null;
	private JButton _butttest = null;
	private JButton _buttcancel = null;
	private MIBDataModel _mibdm = null;
	
	public MIBTestPanel(MIBDataModel mibdm)
	{
		try 
		{
			_mibdm = mibdm;
			initialize();
			_lbloid.setText(_mibdm.getOID());
			_lblname.setText(_mibdm.getName());
			fillPortCmb();
		} 
		catch (Exception e)
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	private void initialize() throws JDOMException, IOException
	{
		_butttest = new JButton("Test OID");
		_butttest.addActionListener(this);
		
		_buttcancel = new JButton("Cancel");
		_txttest = new JLabeldComponent("Result: ", JLabeldComponent.TEXTAREA);
		
		_lbloid = new JLabeldComponent("OID: ", JLabeldComponent.LABEL);
		_lblname = new JLabeldComponent("Name: ", JLabeldComponent.LABEL);
		_cmbtestport = new JLabeldComponent("Test Port: ", JLabeldComponent.COMBOBOX);
		if(!_mibdm.getPortRequired())
			_cmbtestport.setVisible(false);
		_cmbswitch = new JXMLComboBox("select Switch: ", JXMLComboBox.SWITCHLIST);
		_cmbswitch.getComboBox().addItemListener(this);
		
        JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.LINE_AXIS));
        buttonpanel.add(Box.createHorizontalGlue());
        buttonpanel.add(_butttest);
        buttonpanel.add(_buttcancel);
		
        this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        this.add(_lblname);
        this.add(_lbloid);
        this.add(_cmbswitch);
        this.add(_cmbtestport);
        this.add(_txttest);
        this.add(buttonpanel);

        
	}
	
	public void addActionListener(ActionListener l)
	{
		_buttcancel.addActionListener(l);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		if(_cmbswitch.getComboBox().getItemCount() == 0) return;
		String retval = "testing...";
		_txttest.setText(retval);
		try 
		{
			String oid = _mibdm.getOID();
			if(_cmbtestport.isVisible())
				oid += "." + Integer.valueOf(_cmbtestport.getText());
			
			Object retob = NetworkFacade.requestData((SwitchDataModel)_cmbswitch.getComboBox().getSelectedItem(), oid);
			if(retob == null)
			{
				retval = "No answer from OID " + oid;
			}
			else
			{
				retval = retob.toString();
				if(retval == "")
					retval = "A Timeout occured! Make sure you have a connection with the selected Switch.";
			}
			_txttest.setText(retval);
		} 
		catch (NetworkException e) 
		{
			_txttest.setText(e.getMessage());
		}
	}

	public void itemStateChanged(ItemEvent e) 
	{
		try
		{
			fillPortCmb();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	private void fillPortCmb() throws NetworkException
	{
		if(_cmbswitch.getComboBox().getSelectedIndex() == -1) return;
		
		for(String port : ((SwitchDataModel)_cmbswitch.getComboBox().getSelectedItem()).getPortList())
		{
			_cmbtestport.getComboBox().addItem(port);
		}
	}
}
